// a library to wrap and simplify api calls
import apisauce from 'apisauce'
import WINDOW from './Windows'
import * as RootNavigation from '../Navigation/RootNavigation.js'
import { AsyncStorage } from 'react-native'

const logout = ()=> {
  AsyncStorage.removeItem('loginUser', (error) => {
      RootNavigation.navigate('Login')
  })
}

// our "constructor"
const create = (baseURL = WINDOW.baseURL) => {
  // ------
  // STEP 1
  // ------
  //
  // Create and configure an apisauce-based api object.
  //
  const api = apisauce.create({
    // base URL is read from the "constructor"
    baseURL,
    // here are some default headers
    headers: {
      'Cache-Control': 'no-cache',
    },
    // 10 second timeout...
    timeout: 10000  
  })
  const checkMonitor = (response) => {
    console.log(response)
    if (response.status == 403) { 
      WINDOW.showToast('您的账号在另外的地方登陆！')
      logout()
    }
  } 
  api.addMonitor(checkMonitor)
  api.addResponseTransform(response => {
    if (response.data) {
      const { resultCode, resultMessage, data } = response.data
      response.data = data
      response.resultCode = resultCode
      response.resultMessage = resultMessage
    }
  })

  
  // ------
  // STEP 2
  // ------
  //
  // Define some functions that call the api.  The goal is to provide
  // a thin wrapper of the api layer providing nicer feeling functions
  // rather than "get", "post" and friends.
  //
  // I generally don't like wrapping the output at this level because
  // sometimes specific actions need to be take on `403` or `401`, etc.
  //   
  // Since we can't hide from that, we embrace it by getting out of the
  // way at this level.
  //
  // ?username=zxd&password=123456
  const appLogin = (username, password) => api.get('login/appLogin?username=' + username + '&password=' + password)
  const getRoomList = (userid) => api.get('air/pad/home/getRoomList?userid='+userid)
  const getDetailsInfoBySceneId = (sceneId) => api.get('air/pad/details/getDetailsInfoBySceneId?sceneId='+sceneId)
  const getEchartsData = (sceneId, sensorType) => api.get('air/pad/details/getEchartsData?sceneId='+ sceneId + '&sensorType=' + sensorType)
  const getRuleInfo = (sceneId) => api.get('air/pad/airSet/getRuleInfo?sceneId='+sceneId)
  const updateRuleById = (rule) => api.put('air/pad/airSet/updateRuleById',rule)
  const getAirCtrlInfoBySceneId = (sceneId) => api.get('air/pad/airSet/getAirCtrlInfoBySceneId?sceneId='+sceneId)  
  const updateAirRun = (jsonObj) => api.post('air/pad/airSet/updateAirRun', { ...jsonObj })
  const checkCtrlSet = (sceneId) => api.get('air/pad/details/checkCtrlSet', { sceneId })
  const getAirAppVersion = () => api.get('version/getAirAppVersion')
  const bindingLoginName = (userid, registerId, type) => api.get('messageSend/bindingLoginName',{userid, registerId, type})
  const relieveLoginName = (userid) => api.get('messageSend/relieveLoginName',{userid})
  const getAirCtrlStatusByAirRunId = (airRunId) => api.get('air/pad/airSet/getAirCtrlStatusByAirRunId',{airRunId})
  // ------
  // STEP 3
  // ------
  //
  // Return back a collection of functions that we would consider our
  // interface.  Most of the time it'll be just the list of all the
  // methods in step 2.
  //
  // Notice we're not returning back the `api` created in step 1?  That's
  // because it is scoped privately.  This is one way to create truly
  // private scoped goodies in JavaScript.
  //
  return {
    // a list of the API functions from step 2
    appLogin,
    getRoomList,
    appLogin,
    getRoomList,
    getDetailsInfoBySceneId,
    getEchartsData,
    getRuleInfo,
    updateRuleById,
    getAirCtrlInfoBySceneId,
    updateAirRun,
    checkCtrlSet,
    getAirAppVersion,
    bindingLoginName,
    relieveLoginName,
    getAirCtrlStatusByAirRunId,
  }

}

// let's return back our create method as the default.
export default {
  create
}
